package com.bokee.archerwq.lab.pattern.visitor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class VisitorDemo {
	public static void main(String[] args) {
		List<Element> list = new ArrayList<Element>();
		list.add(new This());
		list.add(new That());
		list.add(new TheOther());

		Visitor up = new UpVisitor();
		Iterator<Element> it = list.iterator();
		while (it.hasNext()) {
			Element e = it.next();
			e.accept(up);
		}

		Visitor down = new DownVisitor();
		it = list.iterator();
		while (it.hasNext()) {
			Element e = it.next();
			e.accept(down);
		}
	}
}

interface Element {
	public void accept(Visitor v);
}

class This implements Element {
	public String thiss() {
		return "This";
	}

	@Override
	public void accept(Visitor v) {
		v.visit(this);
	}
}

class That implements Element {
	public String that() {
		return "That";
	}

	@Override
	public void accept(Visitor v) {
		v.visit(this);
	}
}

class TheOther implements Element {
	public String theOther() {
		return "TheOther";
	}

	@Override
	public void accept(Visitor v) {
		v.visit(this);
	}
}

interface Visitor {
	public void visit(This e);

	public void visit(That e);

	public void visit(TheOther e);
}

class UpVisitor implements Visitor {
	@Override
	public void visit(This e) {
		System.out.println("do Up on " + e.thiss());
	}

	@Override
	public void visit(That e) {
		System.out.println("do Up on " + e.that());
	}

	@Override
	public void visit(TheOther e) {
		System.out.println("do Up on " + e.theOther());
	}
}

class DownVisitor implements Visitor {
	@Override
	public void visit(This e) {
		System.out.println("do Down on " + e.thiss());
	}

	@Override
	public void visit(That e) {
		System.out.println("do Down on " + e.that());
	}

	@Override
	public void visit(TheOther e) {
		System.out.println("do Down on " + e.theOther());
	}
}
